package com.rednote.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rednote.common.Result;
import com.rednote.common.UserContextHolder;
import com.rednote.entity.Posts;
import com.rednote.entity.UserLikes;
import com.rednote.entity.vo.PostCardVO;
import com.rednote.service.PostsService;
import com.rednote.service.RatingService;
import com.rednote.service.UserLikesService;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import java.util.Map;
import java.util.HashMap;
/**
 * 用户点赞记录表(UserLikes)控制层
 *
 * @author yao suxuan
 * @since 2025-05-24 18:10:30
 */
@RestController
@RequestMapping("/userLikes")
public class UserLikesController {
    @Resource
    private UserLikesService userLikesService;
    @Resource
    private PostsService postsService;
    @Resource
    private RatingService ratingService;
    /**
     * 点赞帖子
     * @param postId 帖子ID
     * @return 点赞结果
     */
    @PostMapping("/posts/{postId}")
    public Result<?> likePost(@PathVariable Integer postId) {
        try {
            int like = userLikesService.like(postId, 0);
            // 构造返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("like", like);
            int userId = UserContextHolder.getUserId();
            ratingService.updateUserPostRating(userId, postId, "LIKE"); // 或"FAVORITE"、"VIEW"、"COMMENT"
            return Result.success("点赞成功", data);
        } catch (Exception e) {
            return Result.fail("点赞失败: " + e.getMessage());
        }
    }

    /**
     * 点赞评论
     * @param commentId 评论ID
     * @return 点赞结果
     */
    @PostMapping("/comments/{commentId}")
    public Result<?> likeComment(@PathVariable Integer commentId) {
        try {
            int like =userLikesService.like(commentId, 1);
            Map<String, Object> data = new HashMap<>();
            data.put("like", like);
            return Result.success("点赞成功",data);
        }
        catch (Exception e) {
            return Result.fail("点赞失败: " + e.getMessage());
        }
    }

    /**
     * 取消点赞帖子
     * @param postId 帖子ID
     * @return 取消点赞结果
     */
    @DeleteMapping("/posts/{postId}")
    public Result<?> unlikePost(@PathVariable Integer postId) {
        try {
            int like = userLikesService.unlike(postId, 0);
            Map<String, Object> data = new HashMap<>();
            data.put("like", like);  
            return Result.success("取消点赞成功",data);
        }
        catch (Exception e) {
            return Result.fail("取消点赞失败: " + e.getMessage());
        }
    }

    /**
     * 取消点赞评论
     * @param commentId 评论ID
     * @return 取消点赞结果
     */
    @DeleteMapping("/comments/{commentId}")
    public Result<?> unlikeComment(@PathVariable Integer commentId) {
        try {
            int like= userLikesService.unlike(commentId, 1);
            Map<String, Object> data = new HashMap<>();
            data.put("like", like);
            return Result.success("取消点赞成功",data);
        }
        catch (Exception e) {
            return Result.fail("取消点赞失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页大小
     * @return 查询结果
     */
    @GetMapping
    public Result<?> list(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer size) {
        try {
            Integer userId = UserContextHolder.getUserId();
            Page<PostCardVO> likedPosts = postsService.getMyLikedPosts(userId, page, size);
            return Result.success(likedPosts);
        } catch (Exception e) {
            return Result.fail("获取点赞过的帖子失败: " + e.getMessage());
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public UserLikes getById(@PathVariable Long id) {
        return userLikesService.getById(id);
    }
    
    /**
     * 新增数据
     *
     * @param userLikes 实体对象
     * @return 新增结果
     */
    @PostMapping
    public boolean save(@RequestBody UserLikes userLikes) {
        return userLikesService.save(userLikes);
    }

    /**
     * 修改数据
     *
     * @param userLikes 实体对象
     * @return 修改结果
     */
    @PutMapping
    public boolean update(@RequestBody UserLikes userLikes) {
        return userLikesService.updateById(userLikes);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public boolean deleteById(@PathVariable Long id) {
        return userLikesService.removeById(id);
    }
}
